package com.anglireports.report.response.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;


public class CommonUtils {
	private static final DecimalFormat df = new DecimalFormat("00000");  

	
	public static boolean isNotEmptyString(String param){
		return (param != null && param.trim().length() != 0);
	}
	
	
	public static String getSeconds2time(long seconds){
		int day = 0;
		int hour = 0;
		day = (int) Math.floor(seconds/86400);
		hour = (int) Math.floor( (seconds-day*86400)/3600 );
		return (day>0?day+"天":"")+(hour>0?hour+"小时":"");
	}
	
	
	public static String getFormat(String hao){  
	    return df.format(Integer.parseInt(hao));  
	}  
	
	public static String getNum(double num,String format){
		DecimalFormat d_f = new DecimalFormat(format);
		return d_f.format(num);
	}

	public static Float getNum(float num,String format){
		DecimalFormat d_f = new DecimalFormat(format);
		return new Float(d_f.format(num));
	}

	public static double getValue(Double value ,String format){
		return Double.parseDouble(getNum(value,format));
	}
	

	public static List<Map<String,Object>> assoc_unique(
			List<Map<String, Object>> list, String property) {
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		List<Object> listKey = new ArrayList<Object>();
		Iterator<Map<String,Object>> iter = list.iterator();
		while(iter.hasNext()){
			Map<String,Object> map = iter.next();
			Object value = map.get(property);
			if(!listKey.contains(value)){
				listKey.add(value);
				resultList.add(map);
			}
		}
		return resultList;
	}
	
	public static String convert(String utfString){  
	    StringBuilder sb = new StringBuilder();  
	    int i = -1;  
	    int pos = 0;  
	      
	    while((i=utfString.indexOf("\\u", pos)) != -1){  
	        sb.append(utfString.substring(pos, i));  
	        if(i+5 < utfString.length()){  
	            pos = i+6;  
	            sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));  
	        }  
	    }  
	      
	    return sb.toString();  
	}  
	
	public static String unicodeToCn(String unicode) {
		StringBuffer string = new StringBuffer();
	    String[] hex = unicode.split("\\\\u");
	    for (int i = 1; i < hex.length; i++) {
	        // 转换出每一个代码点
	        int data = Integer.parseInt(hex[i], 16);
	        // 追加成string
	        string.append((char) data);
	    }
	    return string.toString();
	}
	 
	public static String cnToUnicode(String string) {
		StringBuffer unicode = new StringBuffer();
	    for (int i = 0; i < string.length(); i++) {
	        // 取出每一个字符
	        char c = string.charAt(i);
	        // 转换为unicode
	        unicode.append("\\u" + Integer.toHexString(c));
	    }
	    return unicode.toString();
	}
	
	public static void main(String[] args) {
		String a = "中打扫它";
		String au = CommonUtils.cnToUnicode(a);
		System.out.println(au);
		System.out.println(CommonUtils.unicodeToCn(au));
	}
	
	public static Integer getValue(Integer param){
		if(param!=null){
				return param.intValue();
		}else{
			return null;
		}
	}
	
	public static Long getValue(Long param){
		if(param!=null){
				return param.longValue();
		}else{
			return null;
		}
	}
	
	public static Double getValue(BigDecimal param){
		if(param!=null){
				return param.doubleValue();
		}else{
			return 0d;
		}
	}

	public static Object getValue(Object param,Class clazz){
		if(param==null){
			return null;
		}
		if(param instanceof BigDecimal){
			return CommonUtils.getValueFormBigDecimal((BigDecimal)param, clazz);
		}else{
			if(Double.class == clazz){
				return CommonUtils.getValue((Double)param, "#.00");
			}else if(Long.class == clazz){
				return CommonUtils.getLongValue(param);
			}else if(Integer.class == clazz){
				return CommonUtils.getIntegerValue(param);
			}
			return param;
		}
	}
	
	
	public static Object getLongValue(Object param) {
		if(param instanceof String){
			return Long.parseLong((String)param);
		}if(param instanceof Long){
			return (Long)param;
		}if(param instanceof Integer){
			return ((Integer)param).longValue();
		}else if(param instanceof BigDecimal){
			return getValue((BigDecimal)param,Long.class);
		}
		return null;
	}

	public static Object getIntegerValue(Object param) {
		if(param instanceof String){
			return Integer.parseInt((String)param);
		}else if(param instanceof Long){
			return ((Long)param).intValue();
		}else if(param instanceof Integer){
			return (Integer)param;
		}else if(param instanceof BigDecimal){
			return getValue((BigDecimal)param,Integer.class);
		}
		return null;
	}

	public static Object getDoubleValue(Object param) {
		if(param instanceof String){
			return Double.parseDouble((String)param);
		}if(param instanceof Double){
			return (Double)param;
		}if(param instanceof Integer){
			return ((Integer)param).doubleValue();
		}else if(param instanceof BigDecimal){
			return getValue((BigDecimal)param,Double.class);
		}
		return null;
	}
	
	public static boolean checkValue(String param,Class<?> clazz){
		if(clazz == Integer.class){
			try {
				Integer.parseInt(param);
				return true;
			} catch (NumberFormatException e) {
				return false;
			}
		}else if(clazz == Double.class){
			try {
				Double.parseDouble(param);
				return true;
			} catch (NumberFormatException e) {
				return false;
			}
		}else if(clazz == Long.class){
			try {
				Long.parseLong(param);
				return true;
			} catch (NumberFormatException e) {
				return false;
			}
		}
		return false;
	}
	
	public static Object getValueFormBigDecimal(BigDecimal param,Class clazz){
		if(param!=null){
			if( Double.class == clazz){
				return param.doubleValue();
			}else if(Long.class == clazz){
				return param.longValue();
			}else if(Integer.class == clazz){
				return param.intValue();
			}else{
				return param;
			}
		}else{
			return null;
		}
		
	}


	/**
	 * 请求参数拼接
	 * @param map
	 * @return
	 */
	public static String getParameter(Map<String, String> map) {
		StringBuffer parameter = new StringBuffer();

		Set<String> keyset = map.keySet();
		Iterator<String> iterator = keyset.iterator();
		boolean indexFlag = true;
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			if (indexFlag) {
				parameter.append(key + "=" + map.get(key));
				indexFlag = false;
			} else {
				parameter.append("&" + key + "=" + map.get(key));
			}
		}
		return parameter.toString();
	}

	public static String getRandomString(int length){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<length;i++){
			sb.append((int)(Math.random()*10));
		}
		return sb.toString();
	}
}	
